﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Flexdev.Security
{
    /// <summary>
    /// HashCode Service.
    /// </summary>
    public class HashCodeService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="password"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetHashCode(Stream stream, string password, string key)
        {
            var sb = new StringBuilder();

            byte[] secretkey = new Byte[64];
            var deriveBytes = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(key));
            secretkey = deriveBytes.GetBytes(64);

            stream.Seek(0, SeekOrigin.Begin);

            var myhmacsha256 = new HMACSHA256(secretkey);
            byte[] hash = myhmacsha256.ComputeHash(stream);

            foreach ( var b in hash )
                sb.Append(b.ToString("x2"));

            stream.Seek(0, SeekOrigin.Begin);

            return sb.ToString();
        }
    }
}
